using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class TrainingManager : MonoBehaviour
{
	public static TrainingManagerSettings settings = new TrainingManagerSettings(0f, 1, godMode: false);

	public static int startTimeGame = 0;

	public ManagerZombies manager;

	public string sceneGameOver = string.Empty;

	[Space(6f)]
	public Toggle godMode;

	[Tooltip("Кол-во зомби на сцене")]
	public Slider limitOnStage;

	[Tooltip("Временной промежуток для добавления")]
	public Slider periodAdd;

	public string sceneStart = string.Empty;

	public Button startButton;

	public string sceneMainMenu = string.Empty;

	public Button mainMenuButton;

	public Image zombieIcon;

	private Image[] zombieIcons = new Image[0];

	private Vector2[] startPositionIcons = new Vector2[0];

	private Vector2[] targetPositionIcons = new Vector2[0];

	private StoreWeapons storeWeapon;

	private float lastTimeScale;

	private bool lastStateListener = true;

	private void Awake()
	{
		if (zombieIcon == null)
		{
			return;
		}
		int num = 0;
		zombieIcons = new Image[Mathf.FloorToInt(limitOnStage.maxValue)];
		zombieIcons[0] = zombieIcon;
		for (num = 1; num < zombieIcons.Length; num++)
		{
			zombieIcons[num] = UnityEngine.Object.Instantiate(zombieIcon, Vector3.zero, Quaternion.identity, zombieIcon.transform.parent);
			zombieIcons[num].rectTransform.anchoredPosition = zombieIcon.rectTransform.anchoredPosition;
			zombieIcons[num].name = zombieIcon.name + num.ToString();
			zombieIcons[num].color = Color.Lerp(new Color(0.25f, 0f, 0f, 1f), zombieIcon.color, (float)num / limitOnStage.maxValue);
		}
		zombieIcon.rectTransform.SetAsLastSibling();
		startPositionIcons = new Vector2[zombieIcons.Length];
		startPositionIcons[0] = zombieIcon.rectTransform.anchoredPosition;
		for (num = 1; num < startPositionIcons.Length; num++)
		{
			startPositionIcons[num] = zombieIcon.rectTransform.anchoredPosition;
			startPositionIcons[num] += new Vector2(0f, -Random.Range(0, 5));
		}
		targetPositionIcons = new Vector2[zombieIcons.Length];
		targetPositionIcons[0] = zombieIcon.rectTransform.anchoredPosition;
		for (num = 1; num < targetPositionIcons.Length; num++)
		{
			targetPositionIcons[num] = zombieIcon.rectTransform.anchoredPosition;
			if (num % 2 == 0)
			{
				targetPositionIcons[num] += new Vector2(Mathf.Ceil((float)num / 2f) * 160f, -Random.Range(0, 5));
			}
			else
			{
				targetPositionIcons[num] += new Vector2((0f - Mathf.Ceil((float)num / 2f)) * 160f, -Random.Range(0, 5));
			}
		}
	}

	private void Start()
	{
		Player.godMode = false;
		if (!string.IsNullOrEmpty(sceneGameOver))
		{
			Player.listenersDeath = OnDeathPlayer;
			Player.godMode = settings.godMode;
			storeWeapon = UnityEngine.Object.FindObjectOfType<Canvas>().GetComponentInChildren<StoreWeapons>(includeInactive: true);
			storeWeapon.buttonShow.onClick.AddListener(OnShowWeaponMenu);
			storeWeapon.buttonClose.onClick.AddListener(OnCloseWeaponMenu);
			storeWeapon.BuyAll();
			manager.periodAdd = settings.periodAdd;
			manager.limitOnStage = Mathf.FloorToInt(settings.limitOnStage);
			return;
		}
		if (PlayerPrefsFile.HasKey("training.limitOnStage"))
		{
			settings.periodAdd = PlayerPrefsFile.GetFloat("training.periodAdd", settings.periodAdd);
			settings.limitOnStage = PlayerPrefsFile.GetInt("training.limitOnStage", settings.limitOnStage);
			settings.godMode = PlayerPrefsFile.GetBool("training.godMode", defaultValue: false);
		}
		if (limitOnStage != null && periodAdd != null)
		{
			UpdateValueSlider(settings);
			godMode.isOn = settings.godMode;
			limitOnStage.onValueChanged.AddListener(ChangeLimitStage);
			periodAdd.onValueChanged.AddListener(ChangedPeriodAdd);
			startButton.onClick.AddListener(LoadSceneStart);
			mainMenuButton.onClick.AddListener(LoadSceneMenu);
		}
	}

	public void UpdateValueSlider(TrainingManagerSettings settings)
	{
		limitOnStage.value = settings.limitOnStage;
		periodAdd.value = settings.periodAdd;
		ChangedPeriodAdd(settings.periodAdd);
		ChangeLimitStage(settings.limitOnStage);
	}

	public void ChangeLimitStage(float valueSlider)
	{
		for (int i = 0; i < zombieIcons.Length; i++)
		{
			zombieIcons[i].gameObject.SetActive(i < Mathf.FloorToInt(valueSlider));
		}
	}

	public void ChangedPeriodAdd(float valueSlider)
	{
		valueSlider += 0.1f * periodAdd.maxValue;
		for (int i = 0; i < zombieIcons.Length; i++)
		{
			zombieIcons[i].rectTransform.anchoredPosition = Vector2.Lerp(startPositionIcons[i], targetPositionIcons[i], valueSlider / periodAdd.maxValue);
		}
	}

	public void LoadSceneStart()
	{
		SceneManager.LoadScene(sceneStart);
	}

	public void LoadSceneMenu()
	{
		SceneManager.LoadScene(sceneMainMenu);
	}

	public void OnShowWeaponMenu()
	{
		lastTimeScale = Time.timeScale;
		lastStateListener = AudioListener.pause;
		if (lastTimeScale != 0f)
		{
			Time.timeScale = 0f;
			AudioListener.pause = true;
		}
	}

	public void OnCloseWeaponMenu()
	{
		if (lastTimeScale != 0f)
		{
			Time.timeScale = lastTimeScale;
			AudioListener.pause = lastStateListener;
			lastTimeScale = 0f;
		}
		Player.onStage.SelectWeapon(storeWeapon.selectedMain, storeWeapon.selectedBackup);
	}

	public void OnDeathPlayer()
	{
		Invoke("LoadSceneGameOver", 4f);
	}

	public void LoadSceneGameOver()
	{
		SceneManager.LoadScene(sceneGameOver);
	}

	private void OnDestroy()
	{
		if (manager != null)
		{
			GameResult.SetParametr("scoresTraining", Mathf.FloorToInt(Player.onStage.scores));
			GameResult.SetParametr("timeTraining", GameResult.FullTime(Mathf.FloorToInt(Time.time / Time.timeScale) - startTimeGame));
			GameResult.SetParametr("kills", ManagerZombies.kills.ToString());
		}
		else if (limitOnStage != null)
		{
			settings.periodAdd = periodAdd.value;
			settings.limitOnStage = Mathf.FloorToInt(limitOnStage.value);
			settings.godMode = godMode.isOn;
			startTimeGame = Mathf.FloorToInt(Time.time / Time.timeScale);
			PlayerPrefsFile.SetFloat("training.periodAdd", settings.periodAdd);
			PlayerPrefsFile.SetInt("training.limitOnStage", settings.limitOnStage);
			PlayerPrefsFile.SetBool("training.godMode", settings.godMode);
			PlayerPrefsFile.Save();
			PlayerSkinSwitcher.SwitchSkin();
		}
	}
}
